//
//  HWGameRankListController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/4.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView

class HWGameRankListController: HWBaseViewController {
    var type = "1"
    var tableView:UITableView!
    var tableViewProtocol = HWGameRankListProtocol()
    var mod:HWGameClassifyConfigModel = HWGameClassifyConfigModel()
    var httpManager = HWGameCenterHttpManager()
    var modelArr = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        
        initFrame()
        
        loadData()
        
        addRefresh()
        
        addAction()
    }
}

extension HWGameRankListController {
    private func initUI() -> Void {
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .colorWithHexString(hexString: "#FFFFFF")
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -40
    }
    
    private func initFrame() -> Void {
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

extension HWGameRankListController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}

extension HWGameRankListController {
    func loadData() -> Void {
        tableView.mj_footer?.isHidden = true
        
        httpManager.getGameRankList(t:self.type,typeId: self.mod.id) { models in
            self.tableViewProtocol.models = models
            self.tableView.reloadData()
//
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.tableView.mj_footer?.isHidden = false
                self.modelArr.removeAllObjects()
                self.modelArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.modelArr as? [HWGameRankListModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_footer?.isHidden = true
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
        }
        
    }
    
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshBackNormalFooter()
        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
        tableView.mj_footer?.isHidden = true
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    @objc func loadMoreData() -> () {
        self.tableView.mj_footer?.endRefreshing()
        self.httpManager.getGameRankListFooter(t:self.type,typeId: self.mod.id) { models in
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.modelArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.modelArr as? [HWGameRankListModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_footer?.endRefreshing()
        }
        
    }
}

extension HWGameRankListController {
    func addAction() -> Void {
        tableViewProtocol.didClickItem = {
            model in
            let gameController = HWGameDetailController()
            gameController.gameId = model.id
            self.navigationController?.pushViewController(gameController, animated: true)
        }
    }
}
